﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win.UltraWinGrid;
using WHC.OrderWater.Commons;

namespace CKNetFrameWork
{
    public partial class Frm_CourseArrangeSel : DockContent
    {
        private TrainingDB.BLL.CK_SchdDetail m_bllCK_SchdDetail = new TrainingDB.BLL.CK_SchdDetail();

      
        private int m_nDelIndex = -1;
        private bool m_bInsertFlag = false;
        private Dictionary<string, string> m_dicColName2AliasName = new Dictionary<string, string>();
        private Dictionary<string, string> m_dicColAliasName2Name = new Dictionary<string, string>();
        private DateTime m_dtSelectDate = new DateTime();

        public Frm_CourseArrangeSel()
        {
            InitializeComponent();
        }

        public Frm_CourseArrangeSel(DateTime dtTemp)
        {
            InitializeComponent();
            m_dtSelectDate = dtTemp;
        }
        public int GetSelSchdDetailID()
        {
            return m_nDelIndex;
        }
        private void InitColumnWidth()
        {

            //foreach (string strTemp in m_dicColName2AliasName.Keys)
            //{
            //    ultraGrid1.DisplayLayout.Bands[0].Columns[m_dicColName2AliasName[strTemp]].Width = Frm_Main.m_nDfuColWidth; 

            //}
            //ultraGrid1.DisplayLayout.Bands[0].Columns["时段"].Width = 80;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["星期设置"].Width = 90;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["缺省学时"].Width = 50;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["教师课时费标准"].Width = 60;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["是否排除节假日"].Width = 60;


        }
        private void ShowAllRecordInDataGridView(string strWhere)
        {
            ultraGrid1.DataSource = null;
            DataSet ds = m_bllCK_SchdDetail.GetClassTalbeEx(strWhere);
            foreach (string strTemp in m_dicColName2AliasName.Keys)
            {
                ds.Tables[0].Columns[strTemp].ColumnName = m_dicColName2AliasName[strTemp];
      
            }

            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns[ultraGrid1.DisplayLayout.Bands[0].Columns.Count - 1].Hidden = true;

        }
        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView("");
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            InitColumnWidth();
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);
        }
        private void AddNewRow()
        {
            if (Frm_Main.CheckPerm(this.Text, "增加"))
            {
                UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
            }
            else
            {
                MessageBox.Show("对不起您没有该权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                int nIndex = ultraGrid1.ActiveRow.Index;
                if (nIndex == -1)
                {
                    List<object> lstO = this.ultraGrid1.DisplayLayout.Bands[0].ColumnFilters.All.ToList();

                    string strColumnName;
                    string strColumnValue;
                    SearchCondition condition = new SearchCondition();
                    foreach (object o in lstO)
                    {
                        ColumnFilter cf = (ColumnFilter)o;
                        strColumnName = cf.Column.Key;
                        strColumnValue = cf.FilterConditions.Count > 0 ? ((FilterCondition)cf.FilterConditions.All[0]).CompareValue.ToString() : "";
                        if (strColumnValue != "")
                        {
                            condition = condition.AddCondition(m_dicColAliasName2Name[strColumnName], strColumnValue, SqlOperator.Equal);
                        }
                    }

                    string filter = condition.BuildConditionSql();
                    filter = filter.Replace("Where", "");
                    ShowAllRecordInDataGridView(filter);
                }
            }
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                if (!m_bInsertFlag)
                {
                    AddNewRow();
                    //UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }

        }

      
      

      

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            //int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            //if (iIndex < 0)
            //    return;
            //m_nDelIndex = ultraGrid1.ActiveRow.Index;
        }
         

        private void Frm_ClassRoom_Load_1(object sender, EventArgs e)
        {
            //DateTime dt = DateTime.Now.Date;
            //DateTime dt2 = dt.AddDays(1);
            //DayOfWeek d = dt.DayOfWeek;
            m_dicColName2AliasName.Add("CK_className", "班级名称");
            m_dicColName2AliasName.Add("CK_TeacherName", "老师名称");
            m_dicColName2AliasName.Add("CK_course", "课程名称");
            m_dicColName2AliasName.Add("CK_ClassRoomExEx", "教室名称");
      
            m_dicColName2AliasName.Add("CK_ClassTime", "时间段");
            m_dicColName2AliasName.Add("classDate", "上课日期");
           
            foreach (string strTemp in m_dicColName2AliasName.Keys)
            {
                //ds.Tables[0].Columns[strTemp].ColumnName = m_dicColName2AliasName[strTemp];
                m_dicColAliasName2Name.Add(m_dicColName2AliasName[strTemp], strTemp);
            }

            ShowAllRecordInDataGridView("classDate = '" + m_dtSelectDate + "'");
        }

        private void ultraGrid1_DoubleClickRow(object sender, DoubleClickRowEventArgs e)
        {
            int nIndex = e.Row.Index;
            if (nIndex < 0)
                return;
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
            m_nDelIndex = (int)CurrentUGR.Cells["CK_SchdDetailId"].Value;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

      

    }
}


